package com.zpself.module.算法练习.链表;

/**
 * @author By ZengPeng
 * @Description
 * @date in  2021/1/20 19:13
 * @Modified By
 */
public class 链表反转 {
    public static void main(String[] args) {

        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        n1.next=n2;
        n2.next=n3;
        n3.next=n4;
        ListNode listNode = myAnswer(n1);
        while (listNode!=null){
            System.out.print(listNode.val+"->");
            listNode = listNode.next;
        }
    }

    //链表反转
    public static ListNode myAnswer(ListNode head){
        ListNode result = null;
        while (head!=null){
            ListNode next = head.next;
            head.next =result;
            result = head;
            head = next;
        }
        return result;
    }
}
